package no.ntnu.fp.watch.gui;


import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.DefaultComboBoxModel;

import javax.swing.AbstractListModel;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import no.ntnu.fp.model.Database;
import no.ntnu.fp.model.Platform;
import no.ntnu.fp.model.Well;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class EventLog extends JFrame implements ActionListener {
	
	private final Dimension windowSize = new Dimension(800, 600);
	
	private JPanel mainPanel;
	private JButton falseAlarmButton;
	private JTextPane eventListHeader;
	private JScrollPane jScrollPane1;
	private JButton eventCommentButton;
	private JButton closeButton;
	private JList eventList;
	private Well well;
	private Database database;
	private Platform platform;
	
	
	public EventLog(Well well, Platform p){
		
		this.platform = p;
		this.well = well;
		this.database = new Database();
		
		
		mainPanel = new JPanel();
		AnchorLayout mainPanelLayout = new AnchorLayout();
		mainPanel.setLayout(mainPanelLayout);
		mainPanel.setPreferredSize(new java.awt.Dimension(794, 587));
		mainPanel.setSize(this.windowSize);
		
		this.add(mainPanel);
		{
			eventListHeader = new JTextPane();
			eventListHeader.setEditable(false);
			eventListHeader.setSelectionColor(Color.white);
			mainPanel.add(eventListHeader, new AnchorConstraint(70, 985, 108, 14, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
			eventListHeader.setText("        Status                                                                           Time");
			eventListHeader.setPreferredSize(new java.awt.Dimension(771, 22));
		}
		{
			jScrollPane1 = new JScrollPane();
			mainPanel.add(jScrollPane1, new AnchorConstraint(108, 985, 971, 14, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
			jScrollPane1.setPreferredSize(new java.awt.Dimension(771, 496));
			{
				
				EventLogRenderer rend = new EventLogRenderer();
				EventLogListModel eventListModel = new EventLogListModel(database.getEvents(this.well.getId()),
						this.database);
				
				well.addPropertyChangeListener(eventListModel);
				
				
				
				eventList = new JList(eventListModel);
				jScrollPane1.setViewportView(eventList);
				jScrollPane1.setVerticalScrollBarPolicy(jScrollPane1.VERTICAL_SCROLLBAR_ALWAYS);
				eventList.setCellRenderer(rend);
				//eventList.setPreferredSize(new java.awt.Dimension(751, 521));
				eventList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			}
		}
		{
			closeButton = new JButton();
			closeButton.addActionListener(this);
			mainPanel.add(closeButton, new AnchorConstraint(14, 566, 61, 416, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
			closeButton.setText("Close");
			closeButton.setPreferredSize(new java.awt.Dimension(119, 27));
		}
		{
			eventCommentButton = new JButton();
			mainPanel.add(eventCommentButton, new AnchorConstraint(14, 372, 61, 212, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
			eventCommentButton.setText("Comment Event");
			eventCommentButton.setPreferredSize(new java.awt.Dimension(127, 27));
			eventCommentButton.addActionListener(this);
		}
		{
			falseAlarmButton = new JButton();
			mainPanel.add(falseAlarmButton, new AnchorConstraint(14, 170, 61, 15, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
			falseAlarmButton.setText("False Alarm");
			falseAlarmButton.setPreferredSize(new java.awt.Dimension(123, 27));
			falseAlarmButton.addActionListener(this);
		}

		this.setSize(windowSize);
		this.setPreferredSize(windowSize);
		this.setTitle("Eventlog for Well"+ this.well.getId());
		this.setPreferredSize(windowSize);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.setAlwaysOnTop(true);
		
	}


	@Override
	public void actionPerformed(ActionEvent arg0) {
		Event selectedEvent = (Event)eventList.getSelectedValue();
		if(arg0.getSource() == closeButton){
			this.dispose();
		}
		else if(arg0.getSource() == eventCommentButton){
			if(selectedEvent.getComment().equals("")){
				new EventCommentDialog(platform, selectedEvent);
			}
			
		}
		else if(arg0.getSource() == falseAlarmButton){
			if(selectedEvent != null){
				System.out.println(selectedEvent.getEventid());
				database.setFalseAlarm(selectedEvent.getEventid());
			}
		}
		
			
	}
	
	
	
}
